<?php

class Application_Model_RelationMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Relation');
		}
		return $this->_dbTable;
	}
	
	public function insertAll(Application_Model_Relation $obj)
    {
        $data = array(
            'member_id'   => $obj->getMember_id(),
            'specific_mbm_id' => $obj->getSpecific_mbm_id(),
        	'relation_type' => $obj->getRelation_type(),
        	'creation_time' => $obj->getCreation_time()
        );
        $this->getDbTable()->insert($data);
    }
    
	public function updateAll(Application_Model_Relation $obj)
    {
    	$db = $this->getDbTable();
    	
        $data = array(
        	'member_id'   => $obj->getMember_id(),
            'specific_mbm_id' => $obj->getSpecific_mbm_id(),
        	'relation_type' => $obj->getRelation_type(),
        	'creation_time' => $obj->getCreation_time()
        );
        $db->update($data,array('relation_id = ?' => $obj->getRelation_id()));
    }
    
	public function findByResult($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Relation();
			$entry->setRelation_id($row->relation_id)
				->setMember_id($row->member_id)
				->setSpecific_mbm_id($row->specific_mbm_id)
				->setRelation_type($row->relation_type)
				->setCreation_time($row->creation_time);
			$entries[] = $entry;
		}
		return $entries;
    }
        
	public function findAll()
    {
    	$db = $this->getDbTable();
        $select = $db->select()->from('relation');
    	return $this->findByResult($select);
    }
    
	public function findByMbmIdRelationType($mbmId, $type)
    {
    	if($mbmId != null && $type != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('relation')
	        	->where('member_id = ?', $mbmId)
	            ->where('relation_type = ?', $type);
	            
            $select = $db->select()
			->from(array('a'=>'relation'), array('username' => 'b.username', 'relation_id', 'memebr_id' => 'b.member_id', 
				'specific_mbm_id', 'relation_type', 'creation_time' => 'a.creation_time'))
			->join(array('b'=>'member'),'b.member_id = a.member_id')
			->where('b.member_id = ?', $mbmId)
	        ->where('relation_type = ?', $type);
	    	
	    	$select->setIntegrityCheck(false); 
	    	
	    	$db = $this->getDbTable();
	    	$resultSet = $db->fetchAll($select);
	        $entries   = array();
			foreach ($resultSet as $row) {
				$entry = new Application_Model_Relation();
				$entry->setRelation_id($row->relation_id)
					->setMember_id($row->member_id)
					->setSpecific_mbm_id($row->specific_mbm_id)
					->setRelation_type($row->relation_type)
					->setCreation_time($row->creation_time)
					->setUsername($row->username);
				$entries[] = $entry;
			}
			return $entries;
    	}
    }
    
	public function findByMbmIdSpecificMbmId($mbmId, $specific_mbm_id)
    {
    	if($mbmId != null && $specific_mbm_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('relation')
	        	->where('member_id = ?', $mbmId)
	             ->where('specific_mbm_id = ?', $specific_mbm_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function deleteByRid($relation_id)
    {
        $this->getDbTable()->delete(array(
        		'relation_id = ?' => $relation_id
        ));
    }
}

